﻿using Max.PH4.Shoppgin.Admin.DataAccess;
using Max.PH4.Shoppgin.Admin.Domain;
using Max.PH4.Shoppgin.Admin.IRespository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH4.Shoppgin.Admin.Respository
{
    public class CategoryRespository : ICategoryRespository
    {
        MyDbContext _context;

        public CategoryRespository(MyDbContext context)
        {
            _context = context;
        }

        public int Delete(CategoryEntity entity)
        {
            _context.Remove(entity);
            int result = _context.SaveChanges();
            return result;
        }

        public int Delete(object id)
        {
            var m = _context.Categories.Find(id);
            if (m != null)
            {
                _context.Remove(m);
                int result = _context.SaveChanges();
                return result;
            }
            else
            {
                return -1;
            }
        }

        public int Insert(CategoryEntity entity)
        {
            _context.Add(entity);
            int result = _context.SaveChanges();
            return result;
        }

        public IQueryable<CategoryEntity> Query()
        {
            return _context.Categories;
        }

        public int Update(CategoryEntity entity)
        {
            _context.Update(entity);
            int result = _context.SaveChanges();
            return result;
        }
    }
}
